package com.zhenai.rocyuan.modules.user.service.impl;

import org.springframework.jdbc.core.simple.SimpleJdbcOperations;

import com.zhenai.rocyuan.modules.user.model.LoginLog;
import com.zhenai.rocyuan.modules.user.model.User;
import com.zhenai.rocyuan.modules.user.service.UserService;

public class UserServiceImpl implements UserService {
	
	SimpleJdbcOperations  mainJdbc;
	
	
	
	@Override
	public int newUser(String weixinId) {
		String sql = "insert into  User (weixinId) values (?);";
		try {
			return mainJdbc.update(sql, weixinId);
		} catch (Exception e) {
			return 0;
		}
		
	}
	@Override
	public int regist(User user) {
		String sql = "update  User  set memberId = ? ,bindDate = now() ,registAdvice = ? ,phone = ?  where weixinId  = ?  ;";
		try {
			return mainJdbc.update(sql, user.getMemberId(), user.getWeixinId(), user.getPhone());
		} catch (Exception e) {
			return 0;
		}
		
	}

	
	@Override
	public boolean isRegist(String weixinId) {
		String sql = " select memberId  from User where weixinId = ? ";
		try {
			mainJdbc.queryForInt(sql, weixinId);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public void logLogin(LoginLog log) {
		String sql = " insert into LoginLog ( memberId,weixinId,loginDate,useDevice,lastOperateTime ) values ( ? ,?,?,? ,now() ) ; ";
		try {
			mainJdbc.update(sql, log.getMemberId(), log.getWeixinId(),log.getLoginDate(), log.getUseDevice());
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	
	@Override
	public void updateLastOperateTime(Integer memberId ){
		String sql = " update  LoginLog  set  lastOperateTime = now() where id = (select id from  LoginLog where memberId = ?  order by  lastOperateTime  desc ;) ";
		try {
			mainJdbc.update(sql, memberId);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	

	

}
